package cn.edu.ecnu.gis.client.gadgets;

import cn.edu.ecnu.gis.client.MashupGISEntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;

import cn.edu.ecnu.gis.client.Th3CallService;
import cn.edu.ecnu.gis.client.Th3CallServiceAsync;
import cn.edu.ecnu.gis.client.model.Gadget;
import cn.edu.ecnu.gis.client.model.GadgetClass;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ImageBundle;
import com.google.gwt.user.client.ui.PushButton;

public class MapEditGadget extends Gadget {

    public static interface OperationIcon extends ImageBundle {

        public AbstractImagePrototype addLineIcon();

        public AbstractImagePrototype addPolygonIcon();

        public AbstractImagePrototype addPointIcon();

        public AbstractImagePrototype zoomInIcon();

        public AbstractImagePrototype zoomOutIcon();
    }
    public static OperationIcon operationIcon = (OperationIcon) GWT.create(OperationIcon.class);
    private PushButton addLineBtn = new PushButton(operationIcon.addLineIcon().createImage());
    private PushButton addPolygonBtn = new PushButton(operationIcon.addPolygonIcon().createImage());
    private PushButton addPointBtn = new PushButton(operationIcon.addPointIcon().createImage());
    private PushButton zoomInBtn = new PushButton(operationIcon.zoomInIcon().createImage());
    private PushButton zoomOutBtn = new PushButton(operationIcon.zoomOutIcon().createImage());

    public static class Class extends GadgetClass {

        public Class() {
            super("地图编辑", false);
            this.setGadgetType(2);
        }

        public Gadget newGadget() {
            return new MapEditGadget(this);
        }
    }
    private HorizontalPanel btnsPanel = new HorizontalPanel();
    private Button jumpBtn = new Button("分析", new ClickListener() {

        public void onClick(Widget sender) {
            try {
                GoogleMapsGadget googleMapsGadget = (GoogleMapsGadget) MashupGISEntryPoint.widgetsList.get("Google Map");
                // GadgetClass gadgetClass1 = (GadgetClass) GadgetClass.getClasses().get(3);
                //GoogleMapsGadget googleMapsGadget=(GoogleMapsGadget)gadgetClass1;
                //double lon = Double.parseDouble(Lattb.getText());
                //double lan = Double.parseDouble(Lontb.getText());

                googleMapsGadget.getGMap().addGeoXML("http://meflyup.googlepages.com/routkml.kmz");
                googleMapsGadget.getGMap().setCenter(37.75722, -122.44722);
            // getService().myMethod(wsreturn.getText(), callback);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        // GoogleMapsGadget googleMapsGadget = (GoogleMapsGadget) GadgetClass.getClasses().get(3);

        }
    });

    protected MapEditGadget(Class c) {
        super(c);
        initWidget(btnsPanel);
        btnsPanel.add(addLineBtn);
        btnsPanel.add(addPolygonBtn);
        btnsPanel.add(addPointBtn);
        btnsPanel.add(zoomInBtn);
        btnsPanel.add(zoomOutBtn);
    // Create an asynchronous callback to handle the result.

    }
    final AsyncCallback callback = new AsyncCallback() {

        public void onSuccess(Object result) {
            //   wsreturn.setText((String) result);
        }

        public void onFailure(Throwable caught) {
            //  wsreturn.setText("Communication failed");
        }
    };

    public static Th3CallServiceAsync getService() {
        // Create the client proxy. Note that although you are creating the
        // service interface proper, you cast the result to the asynchronous
        // version of
        // the interface. The cast is always safe because the generated proxy
        // implements the asynchronous interface automatically.
        Th3CallServiceAsync service = (Th3CallServiceAsync) GWT.create(Th3CallService.class);
        // Specify the URL at which our service implementation is running.
        // Note that the target URL must reside on the same domain and port from
        // which the host page was served.
        //
        ServiceDefTarget endpoint = (ServiceDefTarget) service;
        String moduleRelativeURL = GWT.getModuleBaseURL() + "th3callservice";
        endpoint.setServiceEntryPoint(moduleRelativeURL);
        return service;
    }

    public interface BoundImage extends ImageBundle {

        AbstractImagePrototype inbox();
    }

    public AbstractImagePrototype getBoundImage() {
        BoundImage images = (BoundImage) GWT.create(BoundImage.class);
        return images.inbox();
    }
}
